package gui.windows;

import gui.GUIHandler;
import gui.GUIManager;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JCheckBox;

import network.enums.ServerPackets;
import network.requests.LoginRequest;
import app.AppGlobals;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.UnsupportedEncodingException;

import login.LoginHandler;
import java.awt.Window.Type;
import java.awt.Dialog.ModalExclusionType;

public class RegisterWindow {

	public JFrame frmDevpro;
	private JTextField textField_userName;
	private JPasswordField textField_password;
	private JPasswordField textField_password2;

	/**
	 * Create the application.
	 */
	public RegisterWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmDevpro = new JFrame();
		frmDevpro.setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
		frmDevpro.setType(Type.UTILITY);
		frmDevpro.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frmDevpro.setTitle("DevPro - Register");
		frmDevpro.setBounds(100, 100, 312, 220);
		frmDevpro.getContentPane().setLayout(null);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(10, 28, 57, 14);
		frmDevpro.getContentPane().add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(10, 52, 57, 14);
		frmDevpro.getContentPane().add(lblPassword);
		
		JLabel lblServer = new JLabel("Server");
		lblServer.setBounds(30, 108, 57, 14);
		frmDevpro.getContentPane().add(lblServer);
		
		textField_userName = new JTextField();
		textField_userName.setToolTipText("Wrong Login");
		textField_userName.setBounds(77, 25, 207, 20);
		frmDevpro.getContentPane().add(textField_userName);
		textField_userName.setColumns(10);
		
		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"Europe", "USA"}));
		comboBox_1.setBounds(77, 105, 207, 20);
		frmDevpro.getContentPane().add(comboBox_1);
		
		textField_password = new JPasswordField();
		textField_password.setBounds(77, 49, 207, 20);
		frmDevpro.getContentPane().add(textField_password);
		
		JButton btnRegister = new JButton("Register");
		GUIManager.btnRegisterSend = btnRegister;
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(!new String(textField_password.getPassword()).equals("") && !new String(textField_password2.getPassword()).equals("") &&
						!new String(textField_userName.getText()).equals("")){
					
					if(new String(textField_password.getPassword()).equals(new String(textField_password2.getPassword()))){
						GUIManager.btnRegisterSend.setEnabled(false);
						LoginHandler.sendRegister(textField_userName.getText(), new String(textField_password.getPassword()));
					}else{
						GUIHandler.launchRegisterFailedDialog();
					}
				}else{
					GUIHandler.launchRegisterFailedDialog();
				}
			}
		});
		btnRegister.setBounds(112, 152, 82, 23);
		frmDevpro.getContentPane().add(btnRegister);
		
		JButton btnCancel = new JButton("Close");
		
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUIManager.btnRegister.setEnabled(true);
				frmDevpro.dispose();
			}
		});
		btnCancel.setBounds(204, 152, 82, 23);
		frmDevpro.getContentPane().add(btnCancel);
		
		JLabel lblPleaseEnterA = new JLabel("Please enter a Username and Password");
		lblPleaseEnterA.setBounds(77, 11, 201, 14);
		frmDevpro.getContentPane().add(lblPleaseEnterA);
		
		textField_password2 = new JPasswordField();
		textField_password2.setBounds(77, 74, 207, 20);
		frmDevpro.getContentPane().add(textField_password2);
		
		JLabel lblConfirmPassword = new JLabel("Confirm");
		lblConfirmPassword.setBounds(20, 74, 47, 14);
		frmDevpro.getContentPane().add(lblConfirmPassword);
	}
}
